package com.cyz.web.app.thread.countDown;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class ThreadDemo {


    public static void main(String[] args) {

        ThreadDemo threadDemo = new ThreadDemo();

        long start = System.currentTimeMillis();

        threadDemo.test01();
        log.info("计算时间消耗的毫秒值 {}", (System.currentTimeMillis() - start));
    }

    public void test01() {
        AtomicInteger a = new AtomicInteger(1);
        AtomicInteger b = new AtomicInteger(3);
        AtomicInteger c = new AtomicInteger(5);

        CountDownLatch latch = new CountDownLatch(3);

        new Thread(() -> {
            a.addAndGet(1);
            latch.countDown();
        }).start();

        new Thread(() -> {
            b.addAndGet(2);
            latch.countDown();
        }).start();

        new Thread(() -> {
            c.addAndGet(5);
            latch.countDown();
        }).start();

        try {
            latch.await(4, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int a1 = a.get();
        int b1 = b.get();
        int c1 = c.get();
        int res = a1 + b1 + c1;
        log.info("结果是 {}", res);
    }

    public void test02() {
        AtomicInteger a = new AtomicInteger(1);
        AtomicInteger b = new AtomicInteger(3);
        AtomicInteger c = new AtomicInteger(5);

        new Thread(() -> {
            a.addAndGet(1);

        }).start();

        new Thread(() -> {
            b.addAndGet(2);
        }).start();

        new Thread(() -> {
            c.addAndGet(5);
        }).start();

        int a1 = a.get();
        int b1 = b.get();
        int c1 = c.get();
        int res = a1 + b1 + c1;
        log.info("结果是 {}", res);
    }


}
